{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    " \n",
    "img = cv2.imread('gray_171.jpg'); #이미지 불러오기\n",
    "cv2.imshow(\"image\",img[572:685,385:617]); #이미지 보여주기 (윈도우창 이름, 불러온 이미지)\n",
    "k = cv2.waitKey(0)  #키보드 눌림 대기\n",
    "if k == 27:# ESC키\n",
    "    cv2.destroyAllWindows();\n",
    "elif k == ord('s'): #저장하기 버튼\n",
    "    cv2.imwrite(\"test2.png\",img)\n",
    "    cv2.destroyAllWindows();"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import json\n",
    "import cv2\n",
    "file_name='gray_171'\n",
    "with open(file_name+'.json') as json_file:\n",
    "    json_data = json.load(json_file)\n",
    "    top_left_x_y=json_data[0]['topleft']\n",
    "    bottom_right_x_y=json_data[0]['bottomright']\n",
    "    xt=top_left_x_y['x']    \n",
    "    yt=top_left_x_y['y']\n",
    "    xb=bottom_right_x_y['x']\n",
    "    yb=bottom_right_x_y['y']\n",
    "    img = cv2.imread(file_name+'.jpg'); \n",
    "    cv2.imshow(\"image\",img[yt:yb,xt:xb]); \n",
    "    k = cv2.waitKey(0)  \n",
    "    if k==ord('s'):\n",
    "        cv2.imwrite(\"testt.png\",img[yt:yb,xt:xb])\n",
    "        cv2.destroyAllWindows();\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'x': 385, 'y': 572}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "top_left_x_y\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'x': 617, 'y': 685}"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bottom_right_x_y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "385 572 617 685\n"
     ]
    }
   ],
   "source": [
    "print(xt, yt, xb, yb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
